package com.alibaba.cirtus.ots.application.adapter.ability.strategy;

import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.BaselineGetOrganizationByCodes2Request;
import com.epoch.app.bcorder.model.dto.BaselineGetOrganizationByCodes2Response;
import com.epoch.app.bcorder.service.BaseDataService;
import com.epoch.app.crplatformenhance.api.strategyquery.service.StrategyQueryService;
import com.epoch.app.crplatformenhance.enums.StrategyTypeEnum;
import com.epoch.app.crplatformenhance.model.dto.StrategyPageQueryRequest;
import com.epoch.app.crplatformenhance.model.dto.StrategyPageQueryResponse;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 策略查询
 * @author: yuhw
 * @date: 2023年11月13日 17:21
 **/
@Component
public class StrategyQueryAbility {
    @Resource
    private StrategyQueryService strategyQueryService;
    @Resource
    private BaseDataService baseDataService;

    public List<StrategyPageQueryResponse> query(StrategyTypeEnum strategyTypeEnum, String saleOrgCode){
        StrategyPageQueryRequest strategyPageQueryRequest = new StrategyPageQueryRequest();
        strategyPageQueryRequest.setType(strategyTypeEnum.getCode());
        strategyPageQueryRequest.setOrganizationCode(saleOrgCode);
        Result<List<StrategyPageQueryResponse>> listResult = strategyQueryService.pageQuery(strategyPageQueryRequest);
        if (listResult != null && CollectionUtils.isNotEmpty(listResult.getResult())){
            return listResult.getResult();
        }
        return Lists.newArrayList();
    }

    //查询策略, 判断是否需要使用交货单预占库存,
    public boolean avoidDeliveryOrderOccupy(String saleOrgCode){
        if (StringUtils.isBlank(saleOrgCode)){
            return false;
        }
        BaselineGetOrganizationByCodes2Request request = new BaselineGetOrganizationByCodes2Request();
        request.setIds(Lists.newArrayList(saleOrgCode));
        BaselineGetOrganizationByCodes2Response response = baseDataService.baselineGetOrganizationByCodes2(request);
        if (response == null ||CollectionUtils.isEmpty(response.getResult())){
            return false;
        }
        String id = response.getResult().get(0).getId();
        //查询策略, 策略存在则不需要处理交货单预占
        List<StrategyPageQueryResponse> query = query(StrategyTypeEnum.OCCUPY_INVENTORY, id);
        return CollectionUtils.isNotEmpty(query);

    }
}
